﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

namespace FlowersStore
{
	public partial class AdminProducts : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!IsPostBack && ddlCategories!=null)
			{
				ddlCategories.DataBind();
				ProductsSqlDataSource.DataBind();
				FlowerStoreDataContext db = new FlowerStoreDataContext();
				if (Request.QueryString["cat"] == null)
				{
					ddlCategories.SelectedIndex = ddlCategories.Items.Count - 1;
				}
				else
				{
					if (db.Categories.SingleOrDefault(с => с.Category_id == int.Parse(Request.QueryString["cat"].ToString())) == null)
						Response.Redirect("AdminProducts.aspx");
					else
						ddlCategories.SelectedValue = Request.QueryString["cat"].ToString();
				}
			}
		}

		protected void Insert_Click(object sender, EventArgs e)
		{
			Response.Redirect("AdminProductDetails.aspx");
		}

		protected void ddlCategories_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (ddlCategories.SelectedValue == "")
			{
				ProductsSqlDataSource.SelectParameters.Clear();
				ProductsSqlDataSource.DataBind();
			}
			else
			{
				ProductsSqlDataSource.SelectParameters.Clear();
				ProductsSqlDataSource.SelectParameters.Add("CategoryId", ddlCategories.SelectedValue);
				ProductsSqlDataSource.DataBind();
			}
		}

		protected void Delete_Click(object sender, EventArgs e)
		{
			Button btn = sender as Button;
			int productId = int.Parse(btn.CommandArgument);
			using (FlowerStoreDataContext db = new FlowerStoreDataContext())
			{
				var product = (from p in db.Products
							   where p.Product_id == productId
							   select p).SingleOrDefault();
				if (product == null)
					return;
				foreach (var cp in product.CategoryProductM2Ms)
					db.CategoryProductM2Ms.DeleteOnSubmit(cp);
				db.Products.DeleteOnSubmit(product);
				db.SubmitChanges();
			}
			if (ddlCategories.SelectedValue != "")
				Response.Redirect("AdminProducts.aspx?cat=" + ddlCategories.SelectedValue);
			else
				Response.Redirect("AdminProducts.aspx");

		}

		protected void DeleteFromCat_Click(object sender, EventArgs e)
		{
			Button btn = sender as Button;
			long productId = long.Parse(btn.CommandArgument);
			if (ddlCategories.SelectedValue == "")
			{
				Delete_Click(sender, e);
				return;
			}
			long catId = long.Parse(ddlCategories.SelectedValue);
			using (FlowerStoreDataContext db = new FlowerStoreDataContext())
			{
				var catProd = (from cp in db.CategoryProductM2Ms
							   where cp.Product_id == productId && cp.Category_id == catId
							   select cp).SingleOrDefault();
				if (catProd == null)
					return;
				db.CategoryProductM2Ms.DeleteOnSubmit(catProd);
				db.SubmitChanges();
			}

			if (ddlCategories.SelectedValue != "")
				Response.Redirect("AdminProducts.aspx?cat=" + ddlCategories.SelectedValue);
			else
				Response.Redirect("AdminProducts.aspx");
		}

		protected void Edit_Click(object sender, EventArgs e)
		{
			Button btn = sender as Button;
			Response.Redirect("AdminProductDetails.aspx?product=" + btn.CommandArgument);
		}
	}
}